<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<!--view层  模板-->
<div id="app">
    <todo>
        <todo-title slot="todo-title" v-bind:title="title"></todo-title>
        <todo-items slot="todo-items" v-for="(item,index) in todoItems"
                    v-bind:item="item" v-bind:index="index" v-bind:key="index"
                    v-on:remove="removeItems(index)"></todo-items>
    </todo>
</div>

<!--1、导入vue.js-->
<script src="https://cdn.jsdelivr.net/npm/vue@2.5.21/dist/vue.min.js"></script>
<script>

    Vue.component("todo",{
        template:
            '<div>\
                <slot name="todo-title"></slot>\
                <ul>\
                    <slot name="todo-items"></slot>\
                </ul>\
            </div>'
    });

    Vue.component('todo-title',{
        props:['title'],
        template:'<div>{{title}}</div>'
    });

    //这里的index，就是数组的下标，使用for循环遍历的时候，可以循环出来！
    Vue.component("todo-items",{
        props:["item","index"],
        template:"<li>{{index}}===={{item}} <button v-on:click='remove'>删除</button> </li>",
        methods:{
            remove: function (index){
                //this.$emit 自定义事件分发
                this.$emit('remove',index);
                // alert("111")
            }
        }
    });

    var vm = new Vue({
        el:"#app",
        //Model：数据
        data:{
            title:"语言",
            todoItems:['Java','Python','Linux']
        },
        methods: {
            removeItems:function (index){
                console.log("删除了" + this.todoItems[index]+"OK");
                this.todoItems.splice(index, 1) //一次删除一个元素
            }
        }
    });
</script>

</body>
</html>